//
//  AuthenGeTiSettleMsgVM.swift
//  kxf
//
//  Created by Mac on 2024/3/8.
//

import Foundation
import UIKit
import SwiftUI

class AuthenGeTiSettleMsgVM: UIView {
    
    var selfHeight = RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*5
    
    var controller = WHBaseViewVC()
    
    var typeArr = [["name":"对私账户","type":"2"]]
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var titleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "结算信息"
        return vm
    }()
    lazy var idNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.titleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "持卡人"
        vm.textField.placeholder = "请输入持卡人姓名"
        vm.textField.keyboardType = .default
//        vm.maxLength = 10
        return vm
    }()
    lazy var bankNoVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.titleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "银行卡号"
        vm.textField.placeholder = "请输入银行卡号"
//        vm.textField.keyboardType = .default
        vm.textField.keyboardType = .numberPad
        vm.maxLength = 30
        return vm
    }()
    lazy var bankTypeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankNoVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "结算账户类型"
        vm.textField.placeholder = "请选择结算账户类型"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.showTypeAlert()
        }
        return vm
    }()
    lazy var bankPrivateNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankTypeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "对私银行名称"
        vm.textField.placeholder = "请填写银行名称"
        vm.textField.keyboardType = .default
        return vm
    }()
    lazy var bankPrivateBranchVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankPrivateNameVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "对私开户支行"
        vm.textField.placeholder = "请选择开户支行"
        vm.showArrowImg()
        vm.tapBlock = {()in
            let vc = AuthenBankBranchListVC()
            vc.tapBlock = {(dict)in
                AuthenCompanyMsgModel.shareInstance.settleBankBranchCodePrivate = "\(dict["branchNo"]as? String ?? "")"
                AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate = "\(dict["branchBankName"]as? String ?? "")"
                self.bankPrivateBranchVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate)
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var bankPhoneVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankPrivateBranchVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "预留手机号"
        vm.textField.placeholder = "请输入预留手机号"
        vm.textField.keyboardType = .numberPad
        vm.maxLength = 11
        return vm
    }()
}

extension AuthenGeTiSettleMsgVM{
    func initUI() {
        addSubview(titleVm)
//        addSubview(idNameVm)
        addSubview(bankNoVm)
        addSubview(bankTypeVm)
        addSubview(bankPrivateNameVm)
        addSubview(bankPrivateBranchVm)
        addSubview(bankPhoneVm)
        
    }
    func updateUIForRefuse() {
        bankNoVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNo
        bankTypeVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.settleBankTypeName)")
        bankPrivateNameVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate
        bankPrivateBranchVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate)")
        bankPhoneVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankPhone
    }
    func showTypeAlert(){
        let alert = UIAlertController(title: "请选择结算账户类型", message: nil, preferredStyle: .actionSheet)
        
        for i in 0..<typeArr.count{
            let dict = typeArr[i]as NSDictionary
            let action = UIAlertAction(title: "\(dict["name"]as? String ?? "")", style: .default) { action in
                self.bankTypeVm.setLabelContent(content: "\(dict["name"]as? String ?? "")")
                AuthenCompanyMsgModel.shareInstance.settleBankType =
                dict["type"]as? String ?? ""
                AuthenCompanyMsgModel.shareInstance.settleBankTypeName =
                dict["name"]as? String ?? ""
            }
            alert.addAction(action)
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel)
        alert.addAction(cancelAction)
        self.controller.present(alert, animated: true)
    }
}

